package com.school.graduation.ui.activity.mainUi;


import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;

import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.goods.receiver.InternetConnectStatusReceiver;
import com.school.graduation.release.view.GoodsReleaseActivity;
import com.school.graduation.util.LogUtil;
import com.school.graduation.util.UserUtil;
import com.school.graduation.view.SignInActivity;
import com.school.graduation.ui.activity.presenter.MainActivityPresenter;
import com.school.graduation.ui.view.bottombar.BottomBar;
import com.school.graduation.ui.view.bottombar.OnBottomBarClickListener;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements OnBottomBarClickListener,ImplMainActivity{

    @Bind(R.id.main_bottom)
    BottomBar mainBottom;
    @Bind(R.id.add)
    FloatingActionButton add;
    /*@Bind(R.id.add)
    ImageView add;*/

    private MainActivityPresenter presenter;
    private InternetConnectStatusReceiver receiver;

    @Override
    public int getContentView() {
        return R.layout.activity_main_ui;
    }

    @Override
    public void initData() {
        presenter = new MainActivityPresenter(this,this);
        presenter.setFragmentManager();  //设置fragment管理器
        if (UserUtil.getCurrentUser(context)!=null){
            if (!UserUtil.hasLogIn) presenter.login();
        }

        //LogUtil.d("cache",UserUtil.getCurrentUser(context).getName());
        initReceiver();
        setDefoutFragment();//设置第一界面为二手界面
        setBottomItemClickListener();  //设置bottombar监听器
    }

    private void initReceiver() {
        receiver = new InternetConnectStatusReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver,filter);
    }

    private void setBottomItemClickListener() {
        mainBottom.setOnBottomBarClickListener(this);
    }

    //设置第一界面为二手商品界面
    public void setDefoutFragment() {
        mainBottom.setFirstImg(R.mipmap.home_ff3502);
        mainBottom.setTextColor(0,R.color.md_red_500);
        presenter.setHomeFragment();
    }

    @OnClick(R.id.add)
    public void clickAdd(){
        if (UserUtil.getCurrentUser(context)==null){
            startActivity(new Intent(MainActivity.this,SignInActivity.class));
            return;
        }
        Intent intent = new Intent(MainActivity.this, GoodsReleaseActivity.class);
        startActivity(intent);
    }
    /*@OnClick(R.id.add)
    public void onClick() {
        if (UserUtil.getCurrentUser(context)==null){
            startActivity(new Intent(MainActivity.this,SignInActivity.class));
            return;
        }
        Intent intent = new Intent(MainActivity.this, GoodsReleaseActivity.class);
        startActivity(intent);
    }*/

    @Override
    public void clickFirstItem() {
        reductionBottom();
        setDefoutFragment();
    }

    @Override
    public void clickSecondItem() {
        reductionBottom();
        mainBottom.setSecondImg(R.mipmap.cascades_ff3502);
        mainBottom.setTextColor(1,R.color.md_red_500);
        presenter.setClassificationFragment();
    }

    @Override
    public void clickFourthItem() {
        if (UserUtil.getCurrentUser(context)==null) {
            startActivity(new Intent(MainActivity.this, SignInActivity.class));
            return;
        }
        reductionBottom();
        mainBottom.setFourthImg(R.mipmap.people_ff3502);
        mainBottom.setTextColor(3,R.color.md_red_500);
        presenter.setMineFragment();
    }

    //将bottom的图标和字体颜色还原为没有点击的样子
    private void reductionBottom() {
        mainBottom.setFirstImg(R.mipmap.home);
        mainBottom.setTextColor(0,R.color.md_grey_600);
        mainBottom.setSecondImg(R.mipmap.cascades);
        mainBottom.setTextColor(1,R.color.md_grey_600);
        mainBottom.setTextColor(2,R.color.md_grey_600);
        mainBottom.setFourthImg(R.mipmap.people);
        mainBottom.setTextColor(3,R.color.md_grey_600);
    }

    @Override
    public FragmentManager getMainFragmentManager(){
        return getSupportFragmentManager();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
